<template>
    <div>
        <el-container>
            <el-header class="inside-header">
                <div>
                    <!-- 面包屑 -->
                    <el-breadcrumb separator=">">
                        <el-breadcrumb-item>企业环保</el-breadcrumb-item>
                        <el-breadcrumb-item>人员管理</el-breadcrumb-item>
                        <el-breadcrumb-item class="breadcrumbColor">企业环保人员管理</el-breadcrumb-item>
                    </el-breadcrumb>
                </div>
            </el-header>
            <el-main>
                <div class="inside-main-box">
                    <el-row class="inside-main" style="background: #fff;padding-top: 10px;height: 46px;">
                        <el-col :span="5" style="margin-left: 20px;">
                            <el-form-item label="人员姓名" size="small">
                                <el-input v-model="form.name" placeholder="请输入姓名或电话或部门" />
                            </el-form-item>
                        </el-col>
                           <el-col :span="6" style="margin-left: 20px;">
                            <el-button size="small" class="sele-but" icon="search" @click="selectQuery">查询</el-button>
                            <el-button size="small" class="empty-but" icon="refresh" @click="clear">重置</el-button>
                            <el-button size="small" :icon="Plus" class="add-but" @click="addDialog = true">新增</el-button>

                        </el-col>
                    </el-row>
                </div>
                <div style="background: #fff;">
                    <el-table :data="tableData" style="width: 100%;">
                        <el-table-column align="center" prop="id" label="序号" width="80" />
                        <el-table-column align="center" prop="name" label="姓名" />
                        <el-table-column align="center" prop="telephone" label="电话" />
                        <el-table-column align="center" prop="position" label="职位" />
                        <el-table-column align="center" prop="department" label="部门" />
                        <el-table-column align="center" prop="service_area" label="服务领域" />
                        <el-table-column align="center" prop="education" label="学历" />
                        <el-table-column align="center" prop="service_content" label="服务内容" />
                        <el-table-column align="center" prop="experience" label="从业经验" />
                        <el-table-column align="center" fixed="right" label="操作" width="120">
                            <template #default="scope">
                                <el-button link type="primary" size="small" icon="edit"  @click="updateDetail(scope.row)">修改</el-button>
                                <el-button link type="danger" size="small" icon="delete" @click="deleteRow(scope.row.id)">删除</el-button>
                            </template>
</el-table-column>
</el-table>
<el-pagination style="margin-top: 20%;display: flex;justify-content: flex-end;" v-model:current-page="currentPage3" v-model:page-size="pageSize3" :small="small" :disabled="disabled" :background="background" layout="prev, pager, next, jumper" :total="1000"
    @size-change="handleSizeChange" @current-change="handleCurrentChange" />
</div>
</el-main>
</el-container>
      <!--新增弹出框-->
      <el-dialog v-model="addDialog" title="新增人员信息" width="40%">
    <div style="width: 80%">
        <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px" class="demo-ruleForm">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="ruleForm.name" style="width:300px" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="电话" prop="telephone">
                <el-input v-model="ruleForm.telephone" style="width:300px" placeholder="请输入" />
            </el-form-item>
          <el-form-item label="职位" prop="position">
            <el-input v-model="ruleForm.position" style="width:300px" placeholder="请输入"  />
          </el-form-item>
          <el-form-item label="部门" prop="department">
            <el-input v-model="ruleForm.department" style="width:300px" placeholder="请输入"  />
          </el-form-item>
          <el-form-item label="服务领域" prop="service_area">
            <el-input v-model="ruleForm.service_area" style="width:300px" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="学历" prop="education">
            <el-input v-model="ruleForm.education"  style="width:300px" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="服务内容" prop="service_content">
            <el-input v-model="ruleForm.service_content" maxlength="300" placeholder="请输入" show-word-limit type="textarea" />
          </el-form-item>
          <el-form-item label="从业经验" prop="experience">
            <el-input v-model="ruleForm.experience" maxlength="300" placeholder="请输入" show-word-limit type="textarea" />
          </el-form-item>
        </el-form>
    </div>
    <template #footer>
        <span class="dialog-footer">
          <el-button class="sele-but" @click="insert"> 确定 </el-button>
          <el-button class="empty-but" @click="addDialog = false">取消</el-button>
        </span>
      </template>
</el-dialog>
      <!--修改弹出框-->
      <el-dialog v-model="updateDialog" title="修改人员信息" width="40%">
    <div style="width: 80%">
        <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px" class="demo-ruleForm">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="obj.name" style="width:300px" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="电话" prop="telephone">
                <el-input v-model="obj.telephone" style="width:300px" placeholder="请输入" />
            </el-form-item>
          <el-form-item label="职位" prop="position">
            <el-input v-model="obj.position" style="width:300px" placeholder="请输入"  />
          </el-form-item>
          <el-form-item label="部门" prop="department">
            <el-input v-model="obj.department" style="width:300px" placeholder="请输入"  />
          </el-form-item>
          <el-form-item label="服务领域" prop="service_area">
            <el-input v-model="obj.service_area" style="width:300px" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="学历" prop="education">
            <el-input v-model="obj.education"  style="width:300px" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="服务内容" prop="service_content">
            <el-input v-model="obj.service_content" maxlength="300" placeholder="请输入" show-word-limit type="textarea" />
          </el-form-item>
          <el-form-item label="从业经验" prop="experience">
            <el-input v-model="obj.experience" maxlength="300" placeholder="请输入" show-word-limit type="textarea" />
          </el-form-item>
        </el-form>
    </div>
    <template #footer>
        <span class="dialog-footer">
          <el-button class="sele-but" @click="update(obj)"> 修改 </el-button>
          <el-button class="empty-but" @click="updateDialog = false">取消</el-button>
        </span>
      </template>
</el-dialog>
</div>
</template>

<script setup>
import {onMounted, reactive, ref} from 'vue'
    import { Plus    } from "@element-plus/icons-vue";
    import  {selectAllEntPerson,insertEntPerson,updateEntPerson,deleteEntPerson} from '@/api/entPersonManage'
    // 路由
    import {
        useRouter
    } from 'vue-router'
    import {query} from "@/api/user";
import {ElMessage, ElMessageBox} from "element-plus";
    const router = useRouter()
onMounted(() => {
  selectAll();
});
        // 查全部
    const selectAll = () => {
      selectAllEntPerson({}).then((res)=>{
        tableData.value = res.data;
      })
    }
    //重置
const clear = () => {
  form.name = ""
  form.telephone=""
  form.department=""
  selectAll();
};
    //根据条件查询
    const selectQuery = () => {
      selectAllEntPerson(form).then((res)=>{
        tableData.value = res.data;
      })
    }
    const size = ref("default");
    const form = reactive({
        name: "",
      // telephone:'',
      // department:""
    });
    const tableData = ref([])
// 新增弹窗 表单校验
const ruleForm = reactive({
  name:"",
  telephone:"",
  position:"",
  department:"",
  service_area:"",
  education:"",
  service_content:"",
  experience:""
});
// 新增
const  addDialog = ref(false)
const insert = () => {
  insertEntPerson(ruleForm).then((res) => {
    if (res.code === 200) {
      ElMessage.success("新增成功");
      selectAll();
      addDialog.value = false;
    } else {
      ElMessage.error("新增失败");
    }
  });
};
//修改
const updateDialog = ref(false)
let obj = reactive({
  name: "",
  id: "",
  telephone: "-",
  position: "",
  department:"",
  service_area:"",
  education:"",
  service_content:"",
  experience:"",
});
const updateDetail = (row) =>{
  updateDialog.value = true;
  obj.id = row.id;
  console.log(obj.id,'obj.id')
  obj.name = row.name;
  obj.telephone = row.telephone;
  obj.position = row.position;
  obj.department = row.department;
  obj.service_area = row.service_area;
  obj.education = row.education;
  obj.service_content = row.service_content;
  obj.experience = row.experience;
}
const update = (row) => {
  updateEntPerson(row).then((res) => {
    if (res.code == 200) {
      ElMessage.success("修改成功");
      selectAll();
      updateDialog.value = false;
    } else {
      ElMessage.error("修改失败");
    }
  });
};
//删除
const deleteRow = (index) => {
  ElMessageBox.confirm("确定要删除吗?")
      .then(() => {
        deleteEntPerson({id:index}).then(res=>{
          selectAll();
        })
      })
};
</script>
<style scoped>
    /* 面包屑字体颜色更改 */

    .breadcrumbColor>>>.el-breadcrumb__inner {
        color: #000;
    }
    /* 内部header */

    .inside-header {
        height: 10px;
        display: flex;
        justify-content: left;
        align-items: center;
    }
    /* 空心按钮样式 */

    .empty-but {
        border: 1px solid #3780b9;
        color: #3780b9;
    }
    /* 实心按钮背景样式 */

    .sele-but {
        background: #3780b9;
        border: 0px;
        border-radius: 2px;
        color: white;
    }

    .add-but {
        background: #dde5fe;
        color: #3780b9;
    }
    /* 分页 */

    .demo-pagination-block {
        display: flex;
        justify-content: flex-end;
        align-items: center;
    }
</style>
